var webUrld = "${web.url}";
var fileuploadUrl = "${fileupload.url}";
$(document).ready(function(){
	var logoParam = {};
	logoParam.iconShowType='logo';
	$.ajax({
		url: webUrld+'sysIconInfo/findSysIconInfoByShowType.do',
		type:'post',
		data:logoParam
	}).done(function(data){
		var result = data.bo;
		var thisIconUrl = result.pictureUrl;
		if(thisIconUrl != null && thisIconUrl != "" && thisIconUrl != undefined){
			$("#logoImg").attr("src",fileuploadUrl+"view"+thisIconUrl);
			if(result.iconWidth != null && result.iconHeight != null){
				$("#logoImg").css({
					width:result.iconWidth+"px",
					height:result.iconHeight+"px"
				});
			}
		}else{
			$("#logoImg").attr("src",webUrld+"/public/images/login/logo.png");
		}
	});
	var loginParam = {};
	loginParam.iconShowType='login';
	$.ajax({
		url: webUrld+'sysIconInfo/findSysIconInfoByShowType.do',
		type:'post',
		data:loginParam
	}).done(function(data){
		var result = data.bo;
//		var thisIconUrl = result.pictureUrl;
//		if(thisIconUrl != null && thisIconUrl != "" && thisIconUrl != undefined){
//			$("#loginbg").attr("src",fileuploadUrl+"view"+thisIconUrl);
//			if(result.iconWidth != null && result.iconHeight != null){
//				$("#loginbg").css({
//					width:result.iconWidth+"px",
//					height:result.iconHeight+"px"
//				});
//			}
//		}else{
//			$("#loginbg").attr("src",webUrld+"/public/images/login/loginbg.png");
//		}
	});
	
    areCookiesEnabled();
    //focus username field
    $("input:visible:enabled:first").focus();

    $('#login').on('click',function(){
    	checkLogin();
    });

    $("input").on('keydown',toLogin);

    function toLogin(event){
        if(event.which==13)
        {
            return checkLogin();
        }
    }

    function checkLogin(){
        var username = $('#username').val();
        if(username==''){
            $('#username').css({'border-color':'#ff0000'});
            $("#userNameError").text("请输入用户名！");
            $('#username').focus();
            return false;
        }else{
        	$("#userNameError").text("");
            $('#username').css({'border-color':''});
        }

        var password = $('#password').val();
        if(password==''){
            $('#password').css({'border-color':'#ff0000'});
            $("#loginError").text("请输入密码！");
            $('#password').focus();
            return false;
        }else{
        	$("#loginError").text("");
            $('#password').css({'border-color':''});
        }
       /* var checkcodeInput = $('#checkcodeInput').val();
        if(checkcodeInput==''){
            $('#checkcodeInput').css({'border-color':'#ff0000'});
            $("#checkCodeError").text("请输入验证码！");
            $('#checkcodeInput').focus();
            return false;
        }else{
        	$("#checkCodeError").text("");
            $('#checkcodeInput').css({'border-color':''});
        }*/
        
        var obj = $('#login');
    	obj.attr("id","");
    	obj.text("登录...");
    	$("#loginError").text("");
    	$("#userNameError").text("");
    	$("#checkCodeError").text("");
        var params = {};
        params.username = username;
    	params.password = password;
    	//params.checkcode = checkcodeInput;
    	var totalCount = 5;
    	$.ajax({
    		url: webUrld+'/login.do',
    		type:'post',
    		data:params
    	}).done(function(data){
    		obj.attr("id","login");
    		obj.text("登录");
    		var result = parseInt(data.bo);
    		if(result == 0){
    			location.href=webUrld+"public/page/main.html";
    			sessionStorage.setItem('src',$('#logoImg')[0].src+","+$("#logoImg").css('height')+","+$("#logoImg").css('width'));
    		}else if(result == 2){
    			var haisheng = totalCount - data.other.count;
    			if(haisheng>0){
    				$("#loginError").text("用户名或密码不正确！今天还剩"+haisheng+"次");
        			$("#checkCode").trigger('click');
    			}else{
    				$("#loginError").text("登录异常，请联系管理员");
        			$("#checkCode").trigger('click');
    			}
    			
    		}else if(result == 3){
    			$("#userNameError").text("用户名不能为空！");
    			$("#checkCode").trigger('click');
    		}else if(result == 4){
    			$("#loginError").text("密码不能为空！");
    			$("#checkCode").trigger('click');
    		}else if(result == 5){
    			$("#loginError").text("验证码不能为空！");
    			$("#checkCode").trigger('click');
    		}else if(result == 6){
    			$("#checkCodeError").text("验证码不正确！");
    			$("#checkCode").trigger('click');
    		}else if(result == 10){
    			$("#checkCodeError").text("账户异常！被锁定，请联系管理员处理");
    			$("#checkCode").trigger('click');
    		}
    	});
    }

    function areCookiesEnabled() {
        $.cookie('cookiesEnabled', 'true');
        var value = $.cookie('cookiesEnabled');
        if (value != undefined) {
            $.removeCookie('cookiesEnabled');
            return true;
        }
        return false;
    }
});
